package first.codeTop.meituan;

/**
 * @ClassName Leetcode912
 * @description:
 * @author: 荣燊
 * @create: 2021-08-01 15:20
 **/
public class Leetcode912 {
    // 排序数组
    public static void main(String[] args) {
        int[] arr = {10,7,2,4,7,62,3,4,2,1,8,9,19};
        sortArray(arr);
        for (int i : arr) {
            System.out.print(i+" ");
        }
    }

    // 快排
    public static int[] sortArray(int[] nums) {
        quickSort(nums, 0, nums.length-1);
        return nums;
    }

    public static void quickSort(int[] nums, int i, int j) {
        if (i > j) {
            return;
        }
        int tmp = nums[i];
        int left = i;
        int right = j;
        while (left < right) {
            while (left < right && nums[right] >= tmp) {
                right--;
            }
            swap(nums, left, right);
            while (left < right && nums[left] <= tmp) {
                left++;
            }
            swap(nums, left, right);
        }
        quickSort(nums, i, left-1);
        quickSort(nums, left+1, j);
    }

    public static void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}
